package com.pxxy.lawconsult.utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.pxxy.lawconsult.bean.ClientUser;
import com.pxxy.lawconsult.bean.LawerUser;
import com.pxxy.lawconsult.bean.User;
import com.pxxy.lawconsult.constant.DBConstant;

public class SetBeanUtils {
	/**
	 * 返回客户对象
	 * 
	 * @param result 传入user查询resultSet
	 * @return
	 * @throws SQLException
	 */
	public static ClientUser loginClientUser(ResultSet result) throws SQLException {
		// 查询客户表
		ResultSet rs = DBUtils.query(DBConstant.TB_CLIENT_TABLE, DBConstant.CLIENT_TABLE_USER_ID,
				result.getInt(DBConstant.USER_TABLE_ID) + "");
		// 创建User对象
		User user = loginUser(result);
		// 创建客户对象
		ClientUser clientUser = new ClientUser();
		if (rs.next()) {
			clientUser.setClientId(rs.getInt(DBConstant.CLIENT_TABLE_CLIENT_ID));
			clientUser.setClientSex(rs.getInt(DBConstant.CLIENT_TABLE_CLIENT_SEX));
			clientUser.setEmail(rs.getString(DBConstant.CLIENT_TABLE_CLIENT_EMAIL));
			clientUser.setNickName(rs.getString(DBConstant.CLIENT_TABLE_NICKNAME));
			clientUser.setSignature(rs.getString(DBConstant.CLIENT_TABLE_SIGNATURE));
			clientUser.setTel(user.getTel());
			clientUser.setPassword(user.getPassword());
			clientUser.setType(user.getType());
			clientUser.setId(user.getId());
			clientUser.setImage(user.getImage());
		}

		return clientUser;
	}
	
	/**
	 * 返回律师对象
	 * @param result
	 * @return
	 * @throws SQLException
	 */
	public static LawerUser loginLawerUser(ResultSet result) throws SQLException {
		// 查询律师表
		ResultSet rs = DBUtils.query(DBConstant.TB_LAWER_TABLE, DBConstant.CLIENT_TABLE_USER_ID,
				result.getInt(DBConstant.USER_TABLE_ID) + "");
		// 创建User对象
		User user = loginUser(result);
		// 创建律师对象
		LawerUser lawerUser = new LawerUser();
		if (rs.next()) {
			lawerUser.setEmail(rs.getString(DBConstant.LAWER_TABLE_LAWER_EMAIL));
			lawerUser.setIntroduction(rs.getString(DBConstant.LAWER_TABLE_INTRODUCTION));
			lawerUser.setLawerAge(rs.getInt(DBConstant.LAWER_TABLE_LAWER_AGE));
			lawerUser.setLawerId(rs.getInt(DBConstant.LAWER_TABLE_LAWER_ID));
			lawerUser.setLawerName(rs.getString(DBConstant.LAWER_TABLE_LAWER_NAME));
			lawerUser.setLawerSex(rs.getInt(DBConstant.LAWER_TABLE_LAWER_SEX));
			lawerUser.setNumber(rs.getString(DBConstant.LAWER_TABLE_NUMBER));
			lawerUser.setOffice(rs.getString(DBConstant.LAWER_TABLE_OFFICE));
			lawerUser.setSpeciality(rs.getString(DBConstant.LAWER_TABLE_SPECIALITY));
			lawerUser.setAddress(rs.getString(DBConstant.LAWER_TABLE_LAWER_ADDRESS));
			lawerUser.setTel(user.getTel());
			lawerUser.setPassword(user.getPassword());
			lawerUser.setType(user.getType());
			lawerUser.setId(user.getId());
			lawerUser.setImage(user.getImage());
			
		}

		return lawerUser;
	}

	
	/**
	 * user对象
	 * @param result
	 * @return
	 * @throws SQLException
	 */
	public static User loginUser(ResultSet result) throws SQLException {
		// 封装user对象
		User user = new User();
		user.setId(result.getInt(DBConstant.USER_TABLE_ID));
		user.setImage(result.getString(DBConstant.USER_TABLE_IMAGE));
		user.setPassword(result.getString(DBConstant.USER_TABLE_PASSWORD));
		user.setTel(result.getString(DBConstant.USER_TABLE_TEL));
		user.setType(result.getInt(DBConstant.USER_TABLE_TYPE));
		
		return user;
	}
}
